#ifndef ULTRASONIC_H_
#define ULTRASONIC_H_


#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include "../wireless_debug/wireless_debug.h"
#include "../pilot/autopilot_interface.h"
#include "../HealthManage/HealthManage.h"
#include "../vision/thread_common_op.h"
#include <semaphore.h>

#include "../filter/filter_j1.h"

#define KS103_SLAVE_ADDRESS		0xE8		//0xD0
#define KS103_REGISTER2_ADDRESS	0x02
#define KS103_DEFAULT_CMD		0xBC

#define KS103_RETRIES	0x01
#define KS103_TIMEOUT	0x02
#define KS103_SCMD		0x03

#define RANGE_100MM_RUS				0x01
#define RANGE_4700MM_RUS			0x2F
#define RANGE_0_2_5M_RMM_NTC		0xB0
#define RANGE_0_2_5M_RMM_TC			0xB4
#define RANGE_0_2_11M_RMM_NTC		0xB8
#define RANGE_0_2_11M_RMM_TC		0xBC
#define RANGE_12CM_2_11M_RMM_NTC	0xBD
#define RANGE_12CM_2_11M_RMM_TC		0xBF

#define T_9B						0xC9
#define T_10B						0xCA
#define T_11B						0xCB
#define T_12B						0xCC

#define ULTRA_ANALYSIS_NUM	4

#define ULTRA_GET_ID_A 0x02
#define ULTRA_GET_ID_B 0x04

struct ks103_adapter{
	unsigned char cmd;
	int timeout;
	int retries;
};


#ifndef HAVE_STRUCT_ULTRA_DATA
#define HAVE_STRUCT_ULTRA_DATA
typedef struct{
	float data;
	int gotten;
}ultra_data_t;
#endif



void* start_KS103_thread(void *arg);


class ultrasonic_c
{
	
public:
	
	ultrasonic_c();
	~ultrasonic_c();
	
	ultra_data_t ultra_data;
	filter_j1_c ultra_filter;
	bool is_on;
	uint8_t ultra_bad_flag;
	int valid_count;
	float filtered_data;
	int confidence_flag;
	
	int open(void);
	void close(void);
	
	int setup(const char *device,int baudrate);
	int get_data(float *_data, int _get_id);
	int get_filtered_data(float *_data, int _get_id);
	void KS103_Thread_Func(void);
	

private:

	pthread_t ultra_tid;	
	void set_ultra_data(float _data);
	float get_ultra_data(int _get_id);
	int check_ultra_data_if_available(int _get_id);
	

	
};




#endif
